/**
 * Settle模块 - 余额管理API
 * @description 余额管理相关的API接口定义
 * @author AI Assistant
 * @since 1.0.0
 */

import request from '@/config/axios'
import type { MerchantBalanceVO, BalanceChangeVO, PageResult } from './types'

/**
 * 获取商户余额信息
 * @param merchantNo 商户编号
 * @returns 商户余额信息
 */
export const getMerchantBalanceApi = (merchantNo: string): Promise<MerchantBalanceVO> => {
  return request.get({ url: `/settle/balance/get?merchantNo=${merchantNo}` })
}

/**
 * 获取商户余额列表
 * @param params 查询参数
 * @returns 商户余额列表
 */
export const getMerchantBalanceListApi = (params: {
  pageNo: number
  pageSize: number
  merchantNo?: string
  minBalance?: number // 最小余额
  maxBalance?: number // 最大余额
}): Promise<PageResult<MerchantBalanceVO>> => {
  return request.get({ url: '/settle/balance/page', params })
}

/**
 * 获取余额变动记录分页列表
 * @param params 分页查询参数
 * @returns 余额变动记录分页数据
 */
export const getBalanceChangePageApi = (params: {
  pageNo: number
  pageSize: number
  merchantNo?: string
  changeType?: number
  createTime?: [string, string]
}): Promise<PageResult<BalanceChangeVO>> => {
  return request.get({ url: '/settle/balance-change/page', params })
}

/**
 * 手动调整商户余额
 * @param data 余额调整数据
 * @returns 调整结果
 */
export const adjustMerchantBalanceApi = (data: {
  merchantNo: string // 商户编号(必填)
  changeType: number // 变动类型: 1-增加 2-减少(必填)
  changeAmount: number // 变动金额(必填)
  remark: string // 调整原因(必填)
}): Promise<boolean> => {
  return request.post({ url: '/settle/balance/adjust', data })
}

/**
 * 冻结商户余额
 * @param merchantNo 商户编号
 * @param freezeAmount 冻结金额
 * @param remark 冻结原因
 * @returns 冻结结果
 */
export const freezeMerchantBalanceApi = (
  merchantNo: string,
  freezeAmount: number,
  remark: string
): Promise<boolean> => {
  return request.post({
    url: '/settle/balance/freeze',
    data: { merchantNo, freezeAmount, remark }
  })
}

/**
 * 解冻商户余额
 * @param merchantNo 商户编号
 * @param unfreezeAmount 解冻金额
 * @param remark 解冻原因
 * @returns 解冻结果
 */
export const unfreezeMerchantBalanceApi = (
  merchantNo: string,
  unfreezeAmount: number,
  remark: string
): Promise<boolean> => {
  return request.post({
    url: '/settle/balance/unfreeze',
    data: { merchantNo, unfreezeAmount, remark }
  })
}

/**
 * 获取余额统计数据
 * @param params 查询参数
 * @returns 统计数据
 */
export const getBalanceStatisticsApi = (params?: {
  merchantNo?: string
  dateRange?: [string, string]
}): Promise<{
  totalMerchants: number // 总商户数
  totalBalance: number // 总余额
  totalAvailableBalance: number // 总可用余额
  totalFrozenBalance: number // 总冻结余额
  avgBalance: number // 平均余额
  maxBalance: number // 最大余额
  minBalance: number // 最小余额
}> => {
  return request.get({ url: '/settle/balance/statistics', params })
}

/**
 * 批量查询商户余额
 * @param merchantNos 商户编号列表
 * @returns 商户余额列表
 */
export const batchGetMerchantBalanceApi = (merchantNos: string[]): Promise<MerchantBalanceVO[]> => {
  return request.post({
    url: '/settle/balance/batch-get',
    data: { merchantNos }
  })
}

/**
 * 导出余额变动记录
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportBalanceChangeApi = (params: {
  merchantNo?: string
  changeType?: number
  createTime?: [string, string]
}): Promise<void> => {
  return request.download({
    url: '/settle/balance-change/export',
    params
  })
}

/**
 * 导出商户余额数据
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportMerchantBalanceApi = (params: {
  merchantNo?: string
  minBalance?: number
  maxBalance?: number
}): Promise<void> => {
  return request.download({
    url: '/settle/balance/export',
    params
  })
}
